//
//  MDDOrderViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/3/31.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "MDDOrderViewController.h"
#import "WSFSlideTitlesView.h"
#import "MDDOrderModel.h"
#import "MDDOrderListCell.h"
#import "MDDOrderDetailViewController.h"

@interface MDDOrderViewController ()<WSFSlideTitlesViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *selfTableView;
@property (nonatomic, strong) WSFSlideTitlesView *titlesTopView;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,strong) NSMutableArray *dataArr;
@end

@implementation MDDOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNavigationItems];
    mNavitationTitle(@"我的订单");
    self.view.backgroundColor = [UIColor AxcUI_colorWithHexCode:@"#EBEBEB"];
    [self.view addSubview:self.titlesTopView];
    self.page = 1;
    self.dataArr = [[NSMutableArray alloc]init];
    self.selfTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
         [self loadMyOrderList];
    }];
    self.selfTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
         [self loadMyOrderList];
    }];
    [self loadMyOrderList];
}

#pragma mark -
#pragma mark - **************** tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MDDOrderListCell * itemCell = mGetTableViewCell(@"mDDOrderListCell");
    MDDOrderModel * orderModel = self.dataArr[indexPath.section];
    [itemCell reloadViewForModel:orderModel];
    itemCell.cancerOrderBlock = ^{//取消订单
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"摩嗒嗒提醒您" message:@"是否取消订单" preferredStyle: UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
            
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
            setDickeyobj(parameters, orderModel.orderNo, @"orderNo");
            setDickeyobj(parameters, @"1", @"add");
            [PPNetworkHelper POST:app_order_cancel parameters:parameters success:^(id responseObject) {
                if (YJPPSUCCESS(responseObject)) {
                    [self.dataArr removeObject:orderModel];
                    [self.selfTableView reloadData];
                }else{
                    
                }
            } failure:^(NSError *error) {
                
            } showLoading:self.view];
        }]];
        //弹出提示框；
        [self presentViewController:alert animated:true completion:nil];
    };
    itemCell.sureOrderBlock = ^{//确认订单
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"摩嗒嗒提醒您" message:@"确认订单收货" preferredStyle: UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
            
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
            setDickeyobj(parameters, orderModel.orderNo, @"orderNo");
            setDickeyobj(parameters, @"1", @"add");
            [PPNetworkHelper POST:app_order_complete parameters:parameters success:^(id responseObject) {
                if (YJPPSUCCESS(responseObject)) {
                    [self.dataArr removeObject:orderModel];
                    [self.selfTableView reloadData];
                }else{
                    
                }
            } failure:^(NSError *error) {
                
            } showLoading:self.view];
        }]];
        //弹出提示框；
        [self presentViewController:alert animated:true completion:nil];
    };
    itemCell.delOrderBlock = ^{//删除订单
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"摩嗒嗒提醒您" message:@"是否删除订单" preferredStyle: UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
            
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
            setDickeyobj(parameters, orderModel.orderNo, @"orderNo");
            setDickeyobj(parameters, @"1", @"add");
            [PPNetworkHelper POST:app_order_delete parameters:parameters success:^(id responseObject) {
                if (YJPPSUCCESS(responseObject)) {
                    [self.dataArr removeObject:orderModel];
                    [self.selfTableView reloadData];
                }else{
                    
                }
            } failure:^(NSError *error) {
                
            } showLoading:self.view];
        }]];
        //弹出提示框；
        [self presentViewController:alert animated:true completion:nil];
    };
    return itemCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [MDDOrderListCell cellHeigtForModel:self.dataArr[indexPath.section]];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MDDOrderModel * orderModel = self.dataArr[indexPath.section];
    MDDOrderDetailViewController * odvc = mGetStoryboardViewController(@"Main", @"mDDOrderDetailViewController");
    odvc.orderNo = orderModel.orderNo;
    NavigationController_pushVC(odvc);
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

#pragma makr -lozyLoad
-(WSFSlideTitlesView *)titlesTopView{
    if (!_titlesTopView) {
        WSFSlideTitlesViewSetting *titlesSetting = [[WSFSlideTitlesViewSetting alloc] init];
        titlesSetting.titlesArr = @[ @"全部订单",@"待发货",@"待收货",@"已完成"];
        titlesSetting.frame = CGRectMake(0, getRectNavAndStatusHight, mScreenWidth, 44);
        titlesSetting.lineHeight = 1.5;
        titlesSetting.lineHidden = NO;
        titlesSetting.spaceLineHidden = YES;
        titlesSetting.lineWidth = mScreenWidth/5;
        titlesSetting.textColor = [UIColor AxcUI_colorWithHexCode:@"#333333"];
        titlesSetting.selectedTextColor = [UIColor AxcUI_colorWithHexCode:@"#FC8963"];
        titlesSetting.lineBottomSpace = 0;
        titlesSetting.lineColor = [UIColor AxcUI_colorWithHexCode:@"#FC8963"];;
//        titlesSetting.showRedPoint = YES;
        _titlesTopView = [[WSFSlideTitlesView alloc] initWithSetting:titlesSetting];
        [_titlesTopView selectButtonAtIndex:self.index];
        _titlesTopView.delegate = self;
    }
    return _titlesTopView;
}
- (void)slideTitlesView:(WSFSlideTitlesView *)titlesView didSelectButton:(UIButton *)button atIndex:(NSUInteger)index{
    if (index == 0) {
        self.orderStatus = -1;
    }else if (index == 1){
        self.orderStatus = 0;
    }else if (index == 2){
        self.orderStatus = 10;
    }else{
        self.orderStatus = 20;
    }
    [self.selfTableView.mj_header beginRefreshing];
}
#pragma mark -
#pragma mark - **************** 数据请求
-(void)loadMyOrderList
{
    if ([self.selfTableView.mj_header isRefreshing]) {
        self.page = 1;
    }
    if ([self.selfTableView.mj_footer isRefreshing]) {
        self.page ++;
    }
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    setDickeyobj(parameters, @"10", @"limit");
    NSString * pageStr = [NSString stringWithFormat:@"%ld",self.page];
    setDickeyobj(parameters, pageStr, @"page");
    if (self.orderStatus == -1) {
        
    }else{
        NSString * orderStatus = [NSString stringWithFormat:@"%ld",self.orderStatus];
        setDickeyobj(parameters, orderStatus, @"orderStatus");
    }
    [PPNetworkHelper POST:app_order_list parameters:parameters success:^(id responseObject) {
        [self.selfTableView.mj_header endRefreshing];
        [self.selfTableView.mj_footer endRefreshing];
        if (self.page == 1 && self.dataArr.count > 0) {
            [self.dataArr removeAllObjects];
        }
        if (YJPPSUCCESS(responseObject)) {
            for (NSDictionary * dataD in responseObject[@"data"][@"list"]) {
                MDDOrderModel * item = [[MDDOrderModel alloc]init];
                [item setValuesForKeysWithDictionary:dataD];
                [self.dataArr addObject:item];
            }
            if ([responseObject[@"data"][@"list"] count] < 10) {
                [self.selfTableView.mj_footer endRefreshingWithNoMoreData];
                
            }
        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        [self.selfTableView.mj_header endRefreshing];
        [self.selfTableView.mj_footer endRefreshing];
    } showLoading:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
